package cn.farm.farm.Controller.CommonController;

import cn.farm.farm.Config.Common.CommonDto.DiseaseExecution;
import cn.farm.farm.Config.Common.CommonDto.PageHelperResult;
import cn.farm.farm.Config.Common.CommonDto.PestExecution;
import cn.farm.farm.Config.Common.CommonDto.Result;
import cn.farm.farm.Config.Common.CommonEnums.PestStateEnum;
import cn.farm.farm.Config.Common.CommonException.NotFoundException;
import cn.farm.farm.Entity.Common.Peststress;
import cn.farm.farm.Service.CommonService.DiseaseService;
import cn.farm.farm.Service.CommonService.PestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
@RequestMapping("/farm/common")
public class dengerController {
    @Autowired
    private DiseaseService diseaseService;
    @Autowired
    private PestService pestService;
    //病害名称模糊查询
    @GetMapping(value = "/disease/foundbyname")
    public Result<PageHelperResult> diseaseFound(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "pageNum") Integer PageNum,
            @RequestParam(value = "pageSize") Integer PageSize
    ) throws Exception{
        Result<PageHelperResult> result;//返回信息
        DiseaseExecution execution;//返回Data
            result = diseaseService.foundByName(name, PageNum, PageSize);//获取只含有这三个属性的plant对象的集合
            return result;
    }

    //虫害名称模糊检索
    @PostMapping(value = "/pest/foundbyname")
    public Result<PageHelperResult> pestFound(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "pageNum") Integer PageNum,
            @RequestParam(value = "pageSize") Integer PageSize
    )throws Exception {
        Result<PageHelperResult> result;//返回信息
        PestExecution execution;//返回Data
        try {
            result = pestService.foundByName(name, PageNum, PageSize);//获取只含有这三个属性的plant对象的集合
            return result;
        }catch (Exception e) {
            execution = new PestExecution(PestStateEnum.DEFAULT);
            result = new Result(execution.getState(), execution.getStateInfo());
            return result;
        }

    }

    //虫害联动查询
    @PostMapping(value = "/pest/foundbycate")
    public Result foundpestbycate(
            @RequestParam(value = "kingdom") String kingdom,//界
            @RequestParam(value = "phylum") String phylum,//门
            @RequestParam(value = "class") String bio_class,//纲
            @RequestParam(value = "bioOrder") String bio_order,//目
            @RequestParam(value = "family") String family,//科
            @RequestParam(value = "genus") String genus,//属
            @RequestParam(value = "species") String species,//种
            @RequestParam(value = "pagenum") Integer pageNum,//
            @RequestParam(value = "pagesize") Integer pageSize//
    ) throws Exception{
        PageHelperResult<Peststress> pests;
        Result<PageHelperResult> result;
        PestExecution execution;


        try {
            //获取数据
            pests = pestService.foundByCate(species, pageNum, pageSize);
            execution = new PestExecution(PestStateEnum.SUCCESS);
            result = new Result(execution.getState(), pests, execution.getStateInfo());
            return result;
        } catch (NotFoundException c) {
            execution = new PestExecution(PestStateEnum.FOUND_NULL);
            result = new Result<>(execution.getState(), execution.getStateInfo());
            return result;
        }catch (Exception e){
            execution = new PestExecution(PestStateEnum.INNER_ERROR);
            result = new Result<>(execution.getState(), execution.getStateInfo());
            return result;
        }
    }

}
